﻿//Prevent the TAB key in boxes
function checkTab(){
  if (event.keyCode == 9) { 
	event.returnValue = false;
  }
}
//Go to next box as it reach the max chars
function gotoMax(thisbox,max,maxvalue,next){
    boxvalue=thisbox.value;
	function focusClear(){
		thisbox.value="";
		thisbox.focus();
	}
	//Is a number?
    if (isNaN(boxvalue)){
        alert(OPT_NAN);
				focusClear();
    }
	if (boxvalue>maxvalue){
		alert(OPT_HIGH_VALUE);
		focusClear();
	}
	total=thisbox.value.length;
	if (total==max){
		next.focus();
	}else if(total>max){
		full=thisbox.value;
		cut=full.substring(0,max);
		thisbox.value=cut;
		next.focus();
	}
}
var newbkgtransp; //Global value for new background value
function onOpen() {
  // Get Saved Values
  box_date.focus();
  box_date.value = options.getValue("box_date");
  box_month.value = options.getValue("box_month");
  box_year.value = options.getValue("box_year");
	box_hour.value = options.getValue("box_hour");
	box_minute.value = options.getValue("box_minute");
  box_desc.value = options.getValue("box_desc");
  bkgimg=options.getValue("box_img");
  
  newbkgtransp=options.getValue("box_transp");
  setOpacity(box_img,newbkgtransp);
  switch (newbkgtransp){
    case 10:
     bktrans10.value=true;
     break;
    case 25:
     bktrans25.value=true;
     break;
    case 50:
     bktrans50.value=true;
     break;
    case 100:
     bktrans100.value=true;
     break;
  }

  if (bkgimg==''){
	  bkgimg='stock_images\\noImage.jpg';
  }
  box_img.src = bkgimg;

  //Mac
  if (framework.system.machine.manufacturer == "Apple"){
   setMac();
  }
}
function setDefault(){
	box_img.src='stock_images\\noImage.jpg';
}
function onOk() {
  // Save all the settings
  options.putValue("box_date", box_date.value);
  options.putValue("box_month", box_month.value);
  options.putValue("box_year", box_year.value);
	options.putValue("box_hour", box_hour.value);
	options.putValue("box_minute", box_minute.value);
  options.putValue("box_desc", box_desc.value);
	if (box_img.src=='stock_images\\noImage.jpg' || box_img.src=='' || box_img.src=='noImage.jpg'){
		options.putValue("box_img", '');
	}else{
		options.putValue("box_img", box_img.src);
	}
  options.putValue("box_transp", newbkgtransp);
}

//Background Opacity

function changeBkgTransp() {
  var source = event.srcElement;
  if (source.value) {
    switch (source.name) {
      case 'bktrans10':
        newbkgtransp=10;
        break;
      case 'bktrans25':
        newbkgtransp=25;
        break;
      case 'bktrans50':
        newbkgtransp=50;
        break;
      case 'bktrans100':
        newbkgtransp=100;
        break;
    }
  }
  setOpacity(box_img,newbkgtransp);
}

//Mac options
function setMac(){
   bkg_title.visible=false;
   drag_div.visible=false;
   no_img_link.visible=false;
   label_bkg_transp.visible=false;
   bktrans10.visible=false;
   bktrans25.visible=false;
   bktrans50.visible=false;
   bktrans100.visible=false;
}

/**
 * Drag image here!
 */  
function dragImage() {
  var images = Utils.createDragFilesImagesList(event.dragFiles);
  var numImages = images.length;
  if (numImages>0){
	box_img.src = images[0];
  }
}

/**
 * Utils namespace
 */
var Utils = {};

/**
 * Convert event.dragfiles object to an array of strings
 * @param {Object} obj collection of drag files
 * @return {Array} Array of filepaths
 */
Utils.createDragFilesImagesList = function(obj) {
  var files = [];
  if (!obj) {
    return files;
  }
  var e = new Enumerator(obj);
  var validExtensions = {
    png: true,
    gif: true,
    jpg: true,
    jpeg: true }; 
  while (!e.atEnd()) {
    var path = e.item();
    var extension = Utils.extractExtension(path).toLowerCase();
    if (validExtensions[extension] === true) {
      files.push(path + '');
    }
    e.moveNext();
  }
  return files;
};

/**
 * Extract the extension from a path
 * @param {String} The path
 * @return {String} The extension
 */
Utils.extractExtension = function(s) {
  return s.substring(s.lastIndexOf('.') + 1); 
};